package com.store.mapper;

import com.store.entity.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user ORDER BY create_time DESC")
    List<User> selectList();

    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectById(Long id);

    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUsername(String username);

    @Insert("INSERT INTO user (username, password, role, create_time, update_time) " +
            "VALUES (#{username}, #{password}, #{role}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    @Update("UPDATE user SET username = #{username}, password = #{password}, role = #{role}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int update(User user);

    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteById(Long id);

    @Select("SELECT COUNT(*) FROM user")
    long count();

    @Select("SELECT * FROM user ORDER BY create_time DESC LIMIT #{limit}")
    List<User> selectRecentUsers(int limit);

    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(String username);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);

    @Update("UPDATE user SET last_login_time = #{lastLoginTime} WHERE id = #{id}")
    void updateLastLoginTime(@Param("id") Long id, @Param("lastLoginTime") LocalDateTime lastLoginTime);

    @Update("UPDATE user SET password = #{password}, update_time = #{updateTime} WHERE id = #{id}")
    void updatePassword(@Param("id") Long id, @Param("password") String password);
} 